/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import entity.CategoryEntity;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author nhung_000
 */
public class CategoryDAL extends BaseController<CategoryEntity> {

    @Override
    public List<CategoryEntity> getAll() throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CategoryEntity getById(CategoryEntity t) throws Exception {
        open();
        CategoryEntity e = null;
        String[] param = {t.getId()+""};
        ResultSet rs = select("SELECT * FROM category WHERE id = ?", param);
        if (rs.next()) {
            e = new CategoryEntity(rs.getInt("id"), rs.getString("name"), rs.getString("description"), 
                    rs.getLong("createTime"), rs.getLong("updateTime"), rs.getBoolean("delete"), rs.getBoolean("active"));
        }
        close();
        return e;
    }

    @Override
    public int add(CategoryEntity t) throws Exception {
        open();
        Date aDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Calendar cal = Calendar.getInstance();
        String formattedDate = formatter.format(cal.getTime());
        String[] param = {t.getName(),t.getDescription(),formattedDate,t.getUpdateTime()+"",t.isDelete()+"",t.isActive()+""};
        int run = run("INSERT INTO category(name,description,createTime,updateTime,delete,active)"
                + "VALUES (?,?,?,?,?,?)", param);
        close();
        return run;
    }

    @Override
    public int update(CategoryEntity t) throws Exception {
        open();
        Date aDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Calendar cal = Calendar.getInstance();
        String formattedDate = formatter.format(cal.getTime());
        String[] param = {t.getName(),t.getDescription(),formattedDate,t.getUpdateTime()+"",t.isDelete()+"",t.isActive()+""};
        int run = run("UPDATE category SET name = ?,description = ?,createTime = ?,updateTime = ?,delete = ?,active = ? WHERE id = ?", param);
        close();
        return run;
    }

    @Override
    public int delete(CategoryEntity t) throws Exception {
        open();
        String[] param = {t.getId()+""};
        int run = run("DELETE FROM category WHERE id = ?", param);
        close();
        return run;
    }
    
}
